package 数据结构OJ;

import java.util.ArrayList;

public class 从尾到头打印链表_10 {

    public class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
        }
    }

    public ArrayList<Integer> printListFromTailToHead(ListNode head) {
        ArrayList<Integer> arr = new ArrayList<>();
        if (head == null)
            return arr;
        ListNode prev = null;
        ListNode cur = head;
        ListNode temp;
        while (cur != null) {
            temp = cur;
            cur = cur.next;
            temp.next = prev;
            prev = temp;
        }
        while (prev != null) {
            arr.add(prev.val);
            prev = prev.next;
        }
        return arr;
    }
}